package com.test.device.deviceutil.activity;

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.test.device.deviceutil.R;
import com.test.device.deviceutil.util.DeviceMessageUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.test.device.deviceutil.R.id.txt;

public class MainActivity extends AppCompatActivity {
    private static final int WRITE_EXTERNAL_STORAGE_REQUEST_CODE = 104;
    private  String uu;
    TextView txt;
    @BindView(R.id.resolution)
    Button resolution;
    @BindView(R.id.brand)
    Button brand;
    @BindView(R.id.device_name)
    Button deviceName;
    @BindView(R.id.imei_imsi)
    Button imeiImsi;
    @BindView(R.id.card_number)
    Button cardNumber;
    @BindView(R.id.android_id)
    Button androidId;
    @BindView(R.id.uuid)
    Button uuid;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        txt=findViewById(R.id.txt);
        ButterKnife.bind(this);

        if (Build.VERSION.SDK_INT>=23){
            if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.READ_PHONE_STATE)
                    != PackageManager.PERMISSION_GRANTED) {
                //申请WRITE_EXTERNAL_STORAGE权限
                ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.READ_PHONE_STATE},
                        WRITE_EXTERNAL_STORAGE_REQUEST_CODE);
            } else {

                imeiimsi();
                uu();

            }
        }else {
//这个说明系统版本在6.0之下，不需要动态获取权限。
            imeiimsi();
            uu();
        }

    }



    @OnClick({R.id.resolution, R.id.brand, R.id.device_name, R.id.imei_imsi, R.id.card_number, R.id.android_id})
    public void onViewClicked(View view) {



        switch (view.getId()) {
            case R.id.resolution:
                resolution.setText(DeviceMessageUtil.getResolution(this));
                break;
            case R.id.brand:
                brand.setText(DeviceMessageUtil.getBrand());
                break;
            case R.id.device_name:
                deviceName.setText(DeviceMessageUtil.getDeviceName());
                break;
            case R.id.imei_imsi:
                if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.READ_PHONE_STATE)
                        != PackageManager.PERMISSION_GRANTED) {
                    //申请WRITE_EXTERNAL_STORAGE权限
                    ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.READ_PHONE_STATE},
                            WRITE_EXTERNAL_STORAGE_REQUEST_CODE);
                } else {

                    imeiimsi();

                }
                break;

            case R.id.card_number:
                if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.READ_PHONE_STATE)
                        != PackageManager.PERMISSION_GRANTED) {
                    //申请WRITE_EXTERNAL_STORAGE权限
                    ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.READ_PHONE_STATE},
                            WRITE_EXTERNAL_STORAGE_REQUEST_CODE);
                } else {

                  pn();

                }


                break;
            case R.id.android_id:
                androidId.setText(DeviceMessageUtil.getAndroidId(this));
                break;

            case  R.id.uuid:
                if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.READ_PHONE_STATE)
                        != PackageManager.PERMISSION_GRANTED) {
                    //申请WRITE_EXTERNAL_STORAGE权限
                    ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.READ_PHONE_STATE},
                            WRITE_EXTERNAL_STORAGE_REQUEST_CODE);
                } else {

                   uu();

                }
                break;
        }
    }




    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == WRITE_EXTERNAL_STORAGE_REQUEST_CODE) {
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                // Permission Granted
              //  imeiimsi();
                pn();
                uu();

            }
        }
    }

    private void imeiimsi(){

        imeiImsi.setText(DeviceMessageUtil.getEI_SI("",this));
//        txt.setText(DeviceMessageUtil.getEI_SI("",this));

    }

    private void pn() {

        cardNumber.setText(DeviceMessageUtil.getCard1Number(this));
    }


    private void uu() {
        uuid.setText(DeviceMessageUtil.getUniqueID(this));
        uu=DeviceMessageUtil.getUniqueID(this);
        txt.setText(uu);

    }
}
